﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace Youtube_HD.Model
{
    public class ModelCreator<T>
    {
        /// <summary>
        /// Create an instance from xml format
        /// </summary>
        /// <param name="modelXml">xml data</param>
        /// <returns>An instance of a type</returns>
        public static T s_CreateInstanceFromXml(string modelXml)
        {
            return s_CreateInstanceFromXml(modelXml, null);
        }

        /// <summary>
        /// Create an instance from xml format with name space
        /// </summary>
        /// <param name="modelXml">Xml data</param>
        /// <param name="nameSpace">name space</param>
        /// <returns>An instance of a type</returns>
        public static T s_CreateInstanceFromXml(string modelXml, string nameSpace)
        {
            if (string.IsNullOrEmpty(modelXml)) return default(T);

            try
            {
                var writerSettings = new XmlWriterSettings();
                var namespaces = new XmlSerializerNamespaces();
                namespaces.Add(string.Empty, string.Empty);
                writerSettings.OmitXmlDeclaration = true;

                // convert string to stream
                byte[] byteArray = Encoding.UTF8.GetBytes(modelXml);
                Stream inputStream = new MemoryStream(byteArray);

                var xrs = new XmlReaderSettings { Async = false, CloseInput = true };
                using (XmlReader reader = XmlReader.Create(inputStream, xrs))
                {
                    XmlSerializer serialzer;
                    if (string.IsNullOrEmpty(nameSpace))
                    {
                        serialzer = new XmlSerializer(typeof(T));
                    }
                    else
                    {
                        serialzer = new XmlSerializer(typeof(T), nameSpace);
                    }
                    //serialzer.UnknownAttribute
                    T model = (T)serialzer.Deserialize(reader);
                    return model;
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return default(T);
            }
            finally
            {
                ////Logger.LogVerbose("Finally!");
            }
        }


        /// <summary>
        /// Create an instance from xml format with name space
        /// </summary>
        /// <param name="modelXml">Xml data</param>
        /// <param name="nameSpace">name space</param>
        /// <returns>An instance of a type</returns>
        public static async Task<T> s_CreateInstanceFromXmlAsync(string modelXml, string nameSpace)
        {
            try
            {
                return await Task.Run(() => s_CreateInstanceFromXml(modelXml, nameSpace));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return default(T);
            }
        }
    }
}
